package com.trkj;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import java.lang.reflect.*;


public class MyTest1 {
    private  static Logger logger= LogManager.getLogger(MyTest1.class);

    @Test
    public void test1() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
      Class cls=Emp.class;
      Emp emp=(Emp)cls.newInstance();
      logger.debug("{}",emp);
        Field[] fields=cls.getFields();
        for(Field field:fields){
            logger.debug(field.getName());
        }
        logger.debug("--------");
        fields=cls.getDeclaredFields();//得到所有已定义的属性
        for(Field field : fields){
            logger.debug(field.getName());
        }
        logger.debug("--------");
        Field f=cls.getDeclaredField("ename");
        logger.debug(f.getName());
        f.setAccessible(true);
        f.set(emp,"小王");
        logger.debug(emp.toString());
        logger.debug(f.get(emp));
        // 打印类的包名
        Package pkg = cls.getPackage();
        logger.debug("Package: " + (pkg != null ? pkg.getName() : "default package"));

        // 打印类名
        logger.debug("Class Name: " + cls.getSimpleName());

        // 打印父类
        Class<?> superClass = cls.getSuperclass();
       logger.debug("Superclass: " + (superClass != null ? superClass.getName() : "None"));

        // 打印实现的接口
        Class<?>[] interfaces = cls.getInterfaces();
        if (interfaces.length > 0) {
            logger.debug("Interfaces: ");
            for (Class<?> inter : interfaces) {
                logger.debug(inter.getName() + ", ");
            }
        }
    }
    @Test
    public  void test2(){
        String sql=assemblyupdateSql(Emp.class);
        logger.debug(sql);
    }
    public static String assemblyInsertSql(Class cls){
        //insert into emp(epmno,ename,job,mgr,address) values(?,?,?,?,?);
        StringBuffer sql=new StringBuffer("insert into ");
        sql.append(cls.getSimpleName());
        sql.append("(");
        Field[]fields=cls.getDeclaredFields();
        for (Field field : fields) {
            sql.append(field.getName());
            sql.append(",");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(") values (");
        for (Field field : fields) {
            sql.append("?,");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(")");
        return sql.toString();
    }
    @Test
    public void test3() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class cls=Emp.class;
        Emp emp=(Emp)cls.newInstance();
        Method[] methods=cls.getDeclaredMethods();
        for (Method method : methods) {
            logger.debug(method.getName());
        }
        logger.debug("--------");
        Method ml=cls.getDeclaredMethod("getInfo");
        Parameter[] ps=ml.getParameters();
        logger.debug(ps.length);
        //方法的返回类型
        logger.debug(ml.getReturnType());//得到返回类型
        logger.debug("--------");
        Method ml2=cls.getDeclaredMethod("printInfo", int.class, String.class);
        Parameter[] ps2=ml2.getParameters();//得到方法参数
        logger.debug(ps2.length);
        for (Parameter parameter : ps2) {
            logger.debug("{}",parameter.getType(),parameter.getName());
        }
        //方法的返回类型
        logger.debug(ml2.getReturnType());
        ml2.invoke(emp,10,"aaa");
    }
    @Test
    public void test4() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class cls=Emp.class;
        Constructor[] con= cls.getDeclaredConstructors();
        for (Constructor constructor : con) {
            logger.debug(constructor.getParameters().length);
        }
        Constructor con2=cls.getDeclaredConstructor(int.class,String.class);
        Emp emp=(Emp)con2.newInstance(20,"Tom");
        logger.debug(emp.toString());
    }
    //拼写查询语句
    public static String assemblyselectSql(Class cls){
        //select id,ename,job,mgr,address from emp where id=?;
        StringBuffer sql=new StringBuffer("select ");
        Field[] fields=cls.getDeclaredFields();
        for (Field field : fields) {
            sql.append(field.getName());
            sql.append(",");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(" from ");
        sql.append(cls.getSimpleName());
        sql.append(" where id=?");
        return sql.toString();
    }
    //删除语句拼写
    public static String assemblydeleteSql(Class cls){
        //delete from emp where id=?;
        StringBuffer sql=new StringBuffer("delete ");
        sql.append("from ");
        sql.append(cls.getSimpleName());
        sql.append(" where id=?");
        return sql.toString();
    }
    //修改语句
    public static String assemblyupdateSql(Class cls){
        //update emp set ename=? where id=?;
        StringBuffer sql=new StringBuffer("update ");
        sql.append(cls.getSimpleName());
        sql.append(" set ");
        Field[] fields=cls.getDeclaredFields();
        for (Field field : fields) {
            if(!field.getName().equals("id")) {
                sql.append(field.getName() + "=?");
                sql.append(",");
            }
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(" where id=?");
        return sql.toString();
    }
    @Test
    public void zy() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class cls = User.class;
        User user = (User) cls.newInstance();
        Field f = cls.getDeclaredField("name");
        Field fi=cls.getDeclaredField("id");
        logger.debug(f.getName());
        f.setAccessible(true);
        f.set(user, "小王");
        fi.setAccessible(true);
        fi.set(user,"1");
        logger.debug(user.toString());
        logger.debug(f.get(user));
    }
}
